<?php
namespace app\posts\controller;
use app\forum\controller\UserController;
/**
 * 帖子发布
 */

class PushController extends UserController {

    /**
     * 帖子发布
     */
    public function add()
    {
        $plateId = request('get.plate_id',0,'intval');
        if (empty($plateId)) {
            $this->error404();
        }
        //获取板块信息
        $model = target('forum/Plate');
        if(!empty($plateId)){
            $plateInfo=$model->getInfo($plateId);
        }else{
            $this->error404();
        }
        //信息判断
        if(empty($plateInfo) || strtolower($plateInfo['app'])<>APP_NAME){
            $this->error404();
        }
        //检测权限
        $this->checkForumPurview($plateId,'view,push');

        //获取板块类型
        $subList = target('forum/Plate')->getSubPlate($plateId);
        if($subList){
            $this->error('很抱歉，类目板块无法发布帖子！');
        }
        if(!IS_POST){
            //位置导航
            $crumb = target('forum/Plate')->loadCrumb($plateId);
            $this->crumb = array_merge($this->crumb,$crumb);
            $this->crumb[] = array('url'=>url('posts/Push/add',array('plate_id'=>$plateId)), 'name'=>'发帖');
            $this->assign('plate_id',$plateId);
            $this->assign('url',url('posts/Push/add',array('plate_id'=>$plateId)));
            $this->assign('token',target('forum/Thread')->setToken('thread_add_'.$plateId));
            $this->siteDisplay('index');
        }else{
            //验证token
            if(!target('forum/Thread')->validToken('thread_add_'.$plateId,request('post.token'))){
                $this->error('抱歉，系统安全验证失败，请刷新后重新提交！');
            }
            $_POST['plate_id'] = $plateId;
            $threadId = target('ThreadPosts')->saveData('add','title,content');
            if($threadId){
                //增加积分
                if($plateInfo['integral_add']){
                    target('forum/Integral')->settingInt($this->loginUserInfo['username'],$plateInfo['integral_add'],1,'发帖奖励');
                }
                $this->success('帖子发布成功！',url('posts/Thread/index',array('thread_id'=>$threadId)));
            }else{
                $msg = target('ThreadPosts')->getError();
                if(empty($msg)){
                    $this->error('帖子发布失败');
                }else{
                    $this->error($msg);
                }
            }
        }
    }

    /**
     * 帖子编辑
     */
    public function edit()
    {
        $threadId = request('get.thread_id',0,'intval');
        if (empty($threadId)) {
            $this->error404();
        }
        $threadInfo = target('ThreadPosts')->getInfo($threadId);
        //信息判断
        if(empty($threadInfo)){
            $this->error404();
        }
        //获取板块信息
        $plateInfo = target('forum/Plate')->getInfo($threadInfo['plate_id']);
        //检测板块权限
        $this->checkForumPurview($threadInfo['plate_id'],'view,push');
        //判断编辑权限
        $manage = explode(',', $plateInfo['manage']);
        if(USER_ID <> $threadInfo['post_user'] && !$this->loginUserInfo['admin'] && !in_array($this->loginUserInfo['username'], $manage)){
            $this->error('您没有权限编辑本帖！');
        }
        if(!IS_POST){
            //位置导航
            $crumb = target('forum/Plate')->loadCrumb($threadInfo['plate_id']);
            $this->crumb = array_merge($this->crumb,$crumb);
            $this->crumb[] = array('url'=>url('posts/Push/edit',array('thread_id'=>$threadId)), 'name'=>'编辑');
            $this->assign('info',$threadInfo);
            $this->assign('url',url('posts/Push/edit',array('thread_id'=>$threadId)));
            $this->siteDisplay('index');
        }else{
            $_POST['thread_id'] = $threadId;
            $_POST['plate_id'] = $plateInfo['plate_id'];
            if(target('ThreadPosts')->saveData('edit','title,content')){
                $this->success('帖子编辑成功！',url('posts/Thread/index',array('thread_id'=>$threadId)));
            }else{
                $msg = target('ThreadPosts')->getError();
                if(empty($msg)){
                    $this->error('帖子编辑失败');
                }else{
                    $this->error($msg);
                }
            }
        }
    }
}